package com.lzh.exer2;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author liuzhihao
 * @create 2021-02-23 12:21
 */
public class FileDemo {
    @Test
    public void test1() throws IOException {

        File file = new File("d:\\io\\hello.txt");
        //创建一个与file同目录下的另外一个文件，文件名为：haha.txt
        File destFile = new File(file.getParent(),"haha.txt");
        boolean newFile = destFile.createNewFile();
        if(newFile){
            System.out.println("创建成功");
        }
    }

    @Test
    public void test2(){
        File file = new File("d:\\io");
        String[] list = file.list();
        for(int i= 0;i<list.length;i++){
            boolean b = list[i].endsWith(".txt");
            if(b){
                System.out.println(list[i]);
            }
        }
    }

    @Test
    public void test3(){
        File file = new File("d:\\");
        File[] files = file.listFiles();
        long sum = 0;
        for (int i =0;i< files.length;i++){
            System.out.println(files[i].getName());
            long l = files[i].length();

            sum +=l;
        }
        System.out.println(sum);
    }


}
